package com.shuyihui.rxhttp.utils

import com.shuyihui.rxhttp.func.HandleFuc
import com.shuyihui.rxhttp.func.HttpResponseFunc
import com.shuyihui.rxhttp.model.ApiResult
import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

/**
 *
 * 描述：线程调度工具
 */
object RxUtil {
    @JvmStatic
    fun <T> io_main(): ObservableTransformer<T?, T> {
        return ObservableTransformer { upstream ->
            upstream
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .doOnSubscribe { disposable -> HttpLog.i("+++doOnSubscribe+++" + disposable.isDisposed) }
                .doFinally { HttpLog.i("+++doFinally+++") }
                .observeOn(AndroidSchedulers.mainThread())
        }
    }

    @JvmStatic
    fun <T> _io_main(): ObservableTransformer<ApiResult<T>, T> {
        return ObservableTransformer { upstream ->
            upstream
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(HandleFuc())
                .doOnSubscribe { disposable -> HttpLog.i("+++doOnSubscribe+++" + disposable.isDisposed) }
                .doFinally { HttpLog.i("+++doFinally+++") }
                .onErrorResumeNext(HttpResponseFunc<T>())
        }
    }

    @JvmStatic
    fun <T> _main(): ObservableTransformer<ApiResult<T>, T> {
        return ObservableTransformer { upstream ->
            upstream //.observeOn(AndroidSchedulers.mainThread())
                .map(HandleFuc<T>())
                .doOnSubscribe(Consumer<Disposable> { disposable -> HttpLog.i("+++doOnSubscribe+++" + disposable.isDisposed) })
                .doFinally(Action { HttpLog.i("+++doFinally+++") })
                .onErrorResumeNext(HttpResponseFunc<T>())
        }
    }
}